#include <stdlib.h>

#include "../hle.h"

#include "__InterruptManager.h"

void sceKernelRegisterSubIntrHandler()
{
    s32 intrNum = PARAM(0);
    s32 subIntrNum = PARAM(1);
    u32 funcAddr = PARAM(2);
    u32 argAddr = PARAM(3);
    if (intrNum < 0 || intrNum >= PSP_NUM_INT)
    {
        _log(ERR, HLE, "sceKernelRegisterSubIntrHandler(%d, %d, %08x, %08x): invalid interrupt number", intrNum, subIntrNum, funcAddr, argAddr);
        RETURN(-1);
        return;
    }

    _log(INF, HLE, "0 = sceKernelRegisterSubIntrHandler(%d, %d, %08x, %08x)", intrNum, subIntrNum, funcAddr, argAddr);
    Intr *intr = &intrs[intrNum];
    if (intr->numSubIntrs == 0) {
        intr->subIntrs = malloc(sizeof(SubIntr) * (subIntrNum + 1));
        intr->numSubIntrs = subIntrNum + 1;
    }
    else if (intr->numSubIntrs < subIntrNum + 1) {
        intr->subIntrs = realloc(intr->subIntrs, sizeof(SubIntr) * (subIntrNum + 1));
        intr->numSubIntrs = subIntrNum + 1;
    }
    intr->subIntrs[subIntrNum].funcAddr = funcAddr;
    intr->subIntrs[subIntrNum].argAddr = argAddr;
    intr->subIntrs[subIntrNum].enabled = 0;
    RETURN(0);
}

void sceKernelEnableSubIntr()
{
    s32 intrNum = PARAM(0);
    s32 subIntrNum = PARAM(1);
    if (intrNum < 0 || intrNum >= PSP_NUM_INT)
    {
        _log(ERR, HLE, "sceKernelEnableSubIntr(%d, %d): invalid interrupt number", PARAM(0), PARAM(1));
        RETURN(-1);
        return;
    }
    if (intrs[intrNum].numSubIntrs < subIntrNum + 1)
    {
        _log(ERR, HLE, "sceKernelEnableSubIntr(%d, %d): invalid sub interrupt number", PARAM(0), PARAM(1));
        RETURN(-1);
        return;
    }
    _log(INF, HLE, "0 = sceKernelEnableSubIntr(%d, %d)", PARAM(0), PARAM(1));
    intrs[intrNum].subIntrs[subIntrNum].enabled = 1;
    RETURN(0);
}

void sceKernelDisableSubIntr()
{
    _log(ERR, HLE, "sceKernelDisableSubIntr(%d)", PARAM(0));
    RETURN(0);
}

